home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / ED8N1T2I.ZIP / ED8N1T2I / education / ED8N1T2I / ExamplesFrame.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  9.3 KB  |  252 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // ExamplesFrame.java    v 1.00 b2
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:           v 1.00b1 01-03-1996
  8. //                            v 1.00b2 07-03-1996 Linux choise bug fixed
  9. //                                                Window size adjusted
  10. // Released in public domain: v 1.00b1 01-03-1996
  11. //
  12. // ---- Description ----
  13. // Java class containing methods for the the examples frame.
  14. //
  15. // This program and the Java source is in the public domain.
  16. // Permission to use, copy, modify, and distribute this software
  17. // and its documentation for NON-COMMERCIAL purposes and
  18. // without fee is hereby granted.
  19. //
  20. //    Copyright 1996
  21. //
  22. //    Iwan van Rienen
  23. //    Joan Maetsuyckerstr. 145
  24. //    2593 ZG  The Hague
  25. //    The Netherlands
  26. //
  27. // I am not responsible for any bugs in this program and
  28. // possible damage to hard- or software when using this program.
  29. //****************************************************************************
  30. import java.awt.*;
  31. import java.util.Vector;
  32. import java.io.StringBufferInputStream;
  33. import java.io.DataInputStream;
  34.  
  35. class ExamplesFrame extends Frame {
  36.     DigSim applet;
  37.     Choice c;
  38.     List l;
  39.     Vector ExamplesVector;
  40.     String WaitMessage = "Please wait, loading available examples.";
  41.  
  42. //----------------------------------------------------------------------------
  43. // The constructor of a new examples frame.
  44. //----------------------------------------------------------------------------
  45.     public ExamplesFrame( DigSim app) {
  46.         super("DigSim examples");
  47.         applet = app;
  48.         Button OKButton = new Button ("OK");
  49.         Button CancelButton = new Button ("Cancel");
  50.         OKButton.disable();
  51.  
  52.         ExamplesVector = new Vector();
  53.  
  54.         Panel p1 = new Panel();
  55.         p1.setLayout (new FlowLayout());
  56.         c = new Choice();
  57.         c.addItem ("all");
  58.         p1.add (c);
  59.         p1.add (OKButton);
  60.         p1.add (CancelButton);
  61.         Panel p2 = new Panel();
  62.         p2.setLayout (new GridLayout(0, 1));
  63.         l = new List (10, false);
  64.         l. addItem (WaitMessage);
  65.         p2.add (l);
  66.  
  67.         setLayout(new BorderLayout());
  68.         add ("North", p2);
  69.         add ("South", p1);
  70.  
  71.         resize(400, 275);
  72.         show();
  73.         resize(400, 275);
  74.         LoadExamples();
  75.         ShowCategorys();
  76.         if (c.countItems() > 0) {
  77.             ShowExamples(c.getItem(0));
  78.         }
  79.         l.select(0);
  80.         OKButton.enable();
  81.         p1.layout();
  82.  
  83.     }
  84.  
  85. //----------------------------------------------------------------------------
  86. // Show all available examples in the specified category
  87. //----------------------------------------------------------------------------
  88.     public void ShowExamples (String Category) {
  89.         Example TempExample;
  90.         int ix;
  91.  
  92.         if (ExamplesVector.size() == 0) return; // No examples available
  93.         l.delItems (0, l.countItems() - 1);
  94.         for (ix = 0; ix < ExamplesVector.size(); ix++) {
  95.             TempExample =  (Example) ExamplesVector.elementAt(ix);
  96.             if (Category.equals (TempExample.getType()) || Category.equals("all")) {
  97.                 l.addItem (TempExample.getDescription());
  98.             }
  99.         }
  100.     }
  101.  
  102. //----------------------------------------------------------------------------
  103. // Show all available categorys
  104. //----------------------------------------------------------------------------
  105.     public void ShowCategorys() {
  106.         Example TempExample;
  107.         boolean exist;
  108.         int ix, cl;
  109.  
  110.         if (ExamplesVector.size() == 0) return; // No examples available
  111.         for (ix = 0; ix < ExamplesVector.size(); ix++) {
  112.             TempExample =  (Example) ExamplesVector.elementAt(ix);
  113.             exist = false;
  114.             for (cl = 0; cl < c.countItems(); cl++) {
  115.                 if (c.getItem (cl).equals (TempExample.getType())) {
  116.                     exist = true;
  117.                 }
  118.             }
  119.             if (!exist) {
  120.                 c.addItem (TempExample.getType());
  121.             }
  122.         }
  123.     }
  124.  
  125. //----------------------------------------------------------------------------
  126. // Load a description of all available examples
  127. //----------------------------------------------------------------------------
  128.     public void LoadExamples() {
  129.         String message;
  130.         String ExampleType = null;
  131.         String ExampleDescription = null;
  132.         String ExampleLocation = null;
  133.         Example NewExample;
  134.  
  135.         while (applet.TextFileRequested != null) {
  136.             // File loading in progress for another process.
  137.             // wait a while.
  138.             // System.out.println ("waiting on other process...");
  139.             try {
  140.                 Thread.currentThread().sleep (250);
  141.             } catch(Exception e) { }
  142.         }
  143.         applet.RequestedTextFileRead = false;
  144.         applet.RequestedTextFileError = false;
  145.         applet.TextFileRequested = "examples/description.txt";
  146.         show();
  147.         do {
  148.             // System.out.println ("waiting on data...");
  149.             try {
  150.                 Thread.currentThread().sleep (250);
  151.             } catch(Exception e) { }
  152.         } while (!applet.RequestedTextFileRead && !applet.RequestedTextFileError);
  153.  
  154.         if (applet.RequestedText == null || applet.RequestedTextFileError) {
  155.             applet.TextFileRequested = null;
  156.             String DlgButtons[] = { "OK" };
  157.             message = "Can't read file examples/description.txt";
  158.             SimpleDialog ExceptionDialog = new SimpleDialog(null, "Loading examples", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  159.             return;
  160.         }
  161.         String PlainText = applet.RequestedText;
  162.         applet.TextFileRequested = null;
  163.         StringBufferInputStream sis = new StringBufferInputStream(PlainText);
  164.         DataInputStream dis = new DataInputStream (sis);
  165.         do {
  166.             try {
  167.                 ExampleType = null;
  168.                 ExampleDescription = null;
  169.                 ExampleLocation = null;
  170.  
  171.                 ExampleType = dis.readLine();
  172.                 if (ExampleType != null) {
  173.                     ExampleDescription = dis.readLine();
  174.                 }
  175.                 if (ExampleType != null && ExampleDescription != null) {
  176.                     ExampleLocation = dis.readLine();
  177.                 }
  178.             } catch(Exception e) {
  179.             }
  180.             if (ExampleType != null && ExampleDescription != null && ExampleLocation != null) {
  181.                 NewExample = new Example (ExampleType, ExampleDescription, ExampleLocation);
  182.                 ExamplesVector.addElement(NewExample);
  183.             }
  184.         } while (ExampleType != null && ExampleDescription != null && ExampleLocation != null);
  185.  
  186.     }
  187.  
  188. //----------------------------------------------------------------------------
  189. // Handle the events in this frame.
  190. //----------------------------------------------------------------------------
  191.     public boolean handleEvent(Event ev) {
  192.         if (ev.id == Event.WINDOW_DESTROY) {
  193.             hide();
  194.             applet.MyExamplesFrame = null;
  195.             return true;
  196.         }
  197.         return super.handleEvent(ev);
  198.     }
  199.  
  200. //----------------------------------------------------------------------------
  201. // Get the filename of the example
  202. //----------------------------------------------------------------------------
  203.     public String getFileName(String example) {
  204.         Example TempExample;
  205.         int ix;
  206.  
  207.         for (ix = 0; ix < ExamplesVector.size(); ix++) {
  208.             TempExample =  (Example) ExamplesVector.elementAt(ix);
  209.             if (example.equals (TempExample.getDescription())) {
  210.                 return TempExample.getLocation();
  211.             }
  212.         }
  213.         return null;
  214.     }
  215.  
  216. //----------------------------------------------------------------------------
  217. // Handle all actions in this frame
  218. //----------------------------------------------------------------------------
  219.     public boolean action(Event ev, Object arg) {
  220.         String FileName;
  221.         int s;
  222.  
  223.         // System.out.println ("Action");
  224.         if (ev.target instanceof Button) {
  225.             String label = (String)arg;
  226.             if (label.equals ("Cancel")) {
  227.                 hide();
  228.                 applet.MyExamplesFrame = null;
  229.                 return true;
  230.             } else if (label.equals ("OK")) {
  231.                 s = l.getSelectedIndex();
  232.                 if (s == -1) return true;
  233.                 FileName = getFileName(l.getSelectedItem());
  234.                 hide();
  235.                 applet.UserWantsOpenExample(FileName);
  236.                 applet.MyExamplesFrame = null;
  237.                 return true;
  238.             }
  239.         } else if (ev.target instanceof Choice) {
  240.             ShowExamples((String)arg);
  241.             return true;
  242.         } else if (ev.target instanceof List) {
  243.             if (WaitMessage.equals ((String)arg)) return true;
  244.             FileName = getFileName((String)arg);
  245.             hide();
  246.             applet.UserWantsOpenExample(FileName);
  247.             applet.MyExamplesFrame = null;
  248.             return true;
  249.         }
  250.         return false;
  251.     }
  252. }